<?php

$current_url = strpos($__url,'admin')===0 ? substr($__url,6) : $__url;
//var_dump($__url,$current_url,$items);die;

$have_selected = false;
$index_url = null;

$item_pattern = '<li class="menu-leaf %2$s">%5$s<a href="%1$s" %4$s>%3$s</a>%6$s</li>%7$s';

/**
 * menu
 */
?>
<!-- menu -->
<ul class="menu dropdown dropdown-horizontal clearfix"> 
<?php

switch ($this->user->state) {
default:
	?><li class="menu-leaf menu-pin"><a href="/" class="mlink home-mlink" title="<?php echo __('Home'); ?>"><?php echo __('Home'); ?></a></li><?php
	break;
case 'admin':
	?><li class="menu-leaf menu-pin"><a href="/" class="mlink home-mlink" title="<?php echo __('Frontend'); ?>"><?php echo __('Frontend'); ?></a></li><?php
	?><li class="menu-leaf menu-pin"><a href="/admin/" class="mlink backend-mlink" title="<?php echo __('Backend'); ?>"><?php echo __('Backend'); ?></a></li><?php
	break;
}


// find selected
foreach ( $items as $group => $urls ) :

	if($have_selected) break;

	uksort( $urls, create_function('$a,$b','$a=strlen($a);$b=strlen($b);if($a==$b) return 0; return $a<$b?-1:1;') );

	foreach( $urls as $url => $i ) {
		if($have_selected) break 2;
		$have_selected = $items[$group][$url]['selected'] = isset($i['selected'])&&$i['selected'];
	}

	if( $have_selected ) break;
	foreach( $urls as $url => $i ) {
		if($have_selected) break 2;
		$have_selected = $items[$group][$url]['selected'] = $url==$current_url;
	}

	if( $have_selected ) break;
	foreach( $urls as $url => $i ) {
		if($have_selected) break 2;
		$have_selected = $items[$group][$url]['selected'] = empty($url) ? $current_url==$url : strpos($current_url,$url)===0;
	}

endforeach;


foreach ( $items as $section => $urls ) :

	$count = count( $urls );
	$group = $groups[$section];

	$active = false;
	$active_section = 0;
	$index_active = 0;

	$menu_items = "";
	$index_url = "";

	foreach( $urls as $url => $i ) {

		$active = isset($i['selected'])&&$i['selected'];
		if( $active ) $active_section = 1;

		if( (!empty($i['index']) && $i['index']) || mb_strtolower($i['text']) == "сводная" || strtolower($i['text']) == "index" ) {
			$index_url = site_url( $urlbase.$url );
			$index_active = $active;
			continue;
		}

		$attrs = '';
		foreach( array( 'title', 'target', 'class' ) as $attr )
		if( isset( $i[$attr] ) )
			$attrs .= sprintf( ' %s="%s"', $attr, $this->escape($i[$attr]) );
		
		$url = site_url( $urlbase.$url );
		$class = ($active ? 'selected' : '') . (isset($i['type'])?'menu-'.$i['type']:'');
		$text = $this ->escape( $i['text'] );
		$before = isset($i['before'])	? html_span($i['before'],array('class'=>'menu-item-before')) : '';
		$after	= isset($i['after'])	? html_span($i['after'],array('class'=>'menu-item-after')) : '';
		$extra	= isset($i['extra'])	? html_span($i['extra'],array('class'=>'menu-item-extra')) : '';

		$menu_items .= sprintf( $item_pattern, $url, $class, $text, $attrs, $before, $after, $extra );
	}
	
	$group_title = (mb_strlen($group)>15) ? $group : '';
	$group = (mb_strlen($group)>15) ? texo::acronyme( $group ) : $group;
	if( $index_url )
		$group = sprintf( '<a href="%s" title="%s"><span>%s</span></a>', $index_url, $group_title, $group );
	else
		$group = sprintf( '<a href="#top" title="%s"><span>%s</span></a>', $group_title, $group );


	
	?><li class="menu-branch section-<?php echo $section; ?> <?php if( $active_section ) echo "selected"; ?>"><?php

		/*?><a <?php if($index_active):?> class="selected"<?php endif; ?>><?php*/
			echo $group;
		/*?></dt><?php*/
	
		?><ul class="menu-branch-leafs"><?php
			echo $menu_items;
		?></ul><?php

	?></li><?php

endforeach;


	?><li class="menu-hyphen"><!-- --></li><?

if ($this->user->id):
	?><li class="menu-leaf"><a href="/auth/signout/"><?php echo __('Sign out'); ?></a></li><?
	?><li class="menu-leaf section-name <?php if( strpos($current_url,'profile') === 0 ) { $have_selected = true; echo "selected"; } ?>"><a href="/profile/"><?php echo $this->user->fullname; ?></a></li><?
else:
	?><li class="menu-leaf"><a href="/auth/signin/"><?php echo __('Sign in'); ?></a></li><?
endif;
	
?>
</ul>
<!-- /menu -->